﻿namespace LS.Nc6.Web.Entry.Areas.Systems.Controllers
{
    [Route("[area]/[controller]/[action]")]
    [Area("Systems")]
    [ApiDescriptionSettings("Default")]
    public class OperateLogsController : BaseAdminController
    {
        #region OperateLogsController
       
        private readonly ISysOperateLogService _operatelogService;
        private readonly ICache _cache;
        public OperateLogsController(ISysOperateLogService operatelogService, IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed)
            : base(cacheOptions, resolveNamed)
        {
            _operatelogService = operatelogService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }

        #endregion

        #region 页面
        public ActionResult Index()
        {
            SysPower(SysMenuConstant.MENU_OPERATELOGS_MANAGE, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            return View();
        }
        #endregion

        #region  公共方法
        [HttpGet]
        public async Task<ResponseResultDataList> GetAllLogs()
        {
            string controllerName = RequestHelper.FilterParam(Request.Query["controllerName"]);
            string createtime = RequestHelper.FilterParam(Request.Query["createtime"]);

            var page = Request.Query["page"].ToString().GetQueryValueOrNull<int>(1);
            var limit = Request.Query["limit"].ToString().GetQueryValueOrNull<int>(20);
            string field = Request.Query["field"];
            string order = Request.Query["order"];
            var expable = Expressionable.Create<SysOperateLogEntity>();
            expable.AndIF(!string.IsNullOrEmpty(controllerName), a => a.ControllerName.Contains(controllerName));
         
            if (!string.IsNullOrEmpty(createtime))
            {
                var ls=RequestHelper.GetDateForStr(createtime);
                if (ls.Count >= 2)
                {
                    expable.And(a => a.CreateDate> ls[0].ParseToDateTime() && a.CreateDate<= (ls[1] + " 23:59:59").ParseToDateTime());
                }
            }
            var exp = expable.ToExpression();
            var strField = RequestHelper.FilterParam(field);
            var strOrder = RequestHelper.FilterParam(order);

            Expression<Func<SysOperateLogEntity, object>> expressionOrder;
            if (string.IsNullOrEmpty(strField) || string.IsNullOrEmpty(strOrder))
            {
                expressionOrder = (a) => new { a.Id };
            }
            else
            {
                //实现字段排序
                expressionOrder = (a) => new { a.CreateDate };
            }
            var result = await _operatelogService.PageList(exp, page, limit, expressionOrder);
            return new ResponseResultDataList { code = ResponseHelper.SEUCCESSCODE, message = "请求成功", data = result.Items, count = result.TotalCount };
        }
        #endregion

    }
}